package com.power.ding.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.file.model.FileVo;
import com.plf.core.common.file.FileController;
import com.power.ding.api.ISystemFeign;
import com.power.ding.exception.DingException;
import com.power.ding.model.Di_todo;
import com.power.ding.model.pageInfo.Di_todoPageInfo;
import com.power.ding.service.Di_todoService;
import com.power.ding.service.SystemService;
import com.power.sifa.approve.controller.Aprv_process_materialsController;
import com.power.sifa.approve.model.pageInfo.Aprv_process_materialsPageInfo;
import com.power.sifa.approve.service.Aprv_process_materialsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class SystemController implements ISystemFeign {
    @Autowired
    private SystemService systemService;
    @Autowired
    private Di_todoService di_todoService;


    public ResponseDTO reloadAccessToken() throws DingException {
        String access_token = systemService.loadAccessToken();
        return ResponseDTO.succData(access_token);
    }

    public ResponseDTO getAccessToken() throws DingException {
        String access_token = systemService.getAccessToken();
        return ResponseDTO.succData(access_token);
    }

    /**
     * {
     * "success": true,
     * "content": {
     * "data": {
     * "lastName": "司法局应用", //姓名
     * "realmId": 1, // 租户id
     * "clientId": "nc_sfxzfy", //应用名
     * "openid": "44632630c1c3b4dc13bb112388b3e341", //应用+账号id
     * "realmName": "赣政通租户", //租户名
     * "nickNameCn": "司法局应用", //昵称
     * "tenantUserId": "1$94873", //租户+账号id
     * "employeeCode": "GE__240116165938Prbq9KXch5suH37i2Ha", //租户下人员编码
     * "accountId": 94873, //账号id
     * "tenantName": "赣政通租户",
     * "referId": "94873",
     * "namespace": "local", //账号类型
     * "tenantId": 1,
     * "account": "13697081129" //账号名
     * },
     * "success": true,
     * "responseMessage": "成功",
     * "responseCode": "0"
     * }
     * }
     */
    public ResponseDTO getAppUser(String auth_code) throws DingException {
        JSONObject user = systemService.getAppUser(auth_code);
//        JSONObject user = JSON.parseObject("{\"success\":true,\"content\":{\"data\":{\"lastName\":\"司法局应用\",\"realmId\":1,\"clientId\":\"nc_sfxzfy\",\"openid\":\"44632630c1c3b4dc13bb112388b3e341\",\"realmName\":\"赣政通租户\",\"nickNameCn\":\"司法局应用\",\"tenantUserId\":\"1$94873\",\"employeeCode\":\"GE__240116165938Prbq9KXch5suH37i2Ha\",\"accountId\":94873,\"tenantName\":\"赣政通租户\",\"referId\":\"94873\",\"namespace\":\"local\",\"tenantId\":1,\"account\":\"13697081129\"},\"success\":true,\"responseMessage\":\"成功\",\"responseCode\":\"0\"}}");
        if ((boolean) user.get("success")) {
            return ResponseDTO.succData(((Map) user.get("content")).get("data"));
        }
        return ResponseDTO.succData(user);
    }

    public ResponseDTO getAppToken(String auth_code) {
        // TODO Auto-generated method stub
        return null;
    }

    public ResponseDTO getJsapiToken() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ResponseDTO getOpenAuthorizeUrl(String employeeCode, String redirect) throws DingException {
        String url = systemService.getOpenAuthorizeUrl(employeeCode, redirect);
        return ResponseDTO.succData(url);
    }

    @Override
    public ResponseDTO getOpenUser(String code) throws DingException {
        JSONObject user = systemService.getOpenUser(code);
        return null;
    }

    public ResponseDTO getTime() throws DingException {
        Long timestamp = systemService.getTime();
        return ResponseDTO.succData(timestamp + "");
    }

    public ResponseDTO sendMessage(String id) throws DingException {
        systemService.sendMessage(id);
        return ResponseDTO.succ();
    }

    public ResponseDTO getMessage(String id) throws DingException {
        JSONObject message = systemService.getMessage(id);
        return ResponseDTO.succData(message);
    }

    public ResponseDTO sendTodo(String id) throws DingException {
        systemService.sendTodo(id);
        return ResponseDTO.succ();
    }

    public ResponseDTO getTodo(String id) throws DingException {
        Assert.notNull(id, "id不能为空");
        Map<String, Object> todo = systemService.getTodo(id);
        return ResponseDTO.succData(todo);
    }

    @Override
    public ResponseDTO finishTodo(String id) {
        try {
            this.systemService.finishTodo(id);
            return ResponseDTO.succ();
        } catch (Exception e) {
            log.error("完成待办失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }

    }

	@Override
	public ResponseDTO delTodo(String id) throws DingException {
        Assert.notNull(id, "id不能为空");
        JSONObject message = systemService.delTodo(id);
        return ResponseDTO.succData(message);
	}

}
